// src/main.js import Vue from "vue"; import Cookies from "js-cookie"; import "babel-polyfill"; import Element from "element-ui"; import "./assets/styles/element-variables.scss"; import "@/assets/styles/index.scss"; import "@/assets/styles/ruoyi.scss"; import App from "./App"; import store from "./store"; import router from "./router"; import directive from "./directive"; import plugins from "./plugins"; import { download } from "@/utils/request"; import Print from "vue-print-nb"; import JsonExcel from "vue-json-excel"; import "./assets/icons"; import "./permission"; import { getDicts } from "@/api/system/dict/data"; import { getConfigKey } from "@/api/system/config"; import { parseTime, resetForm, addDateRange, selectDictLabel, selectDictLabels, handleTree, } from "@/utils/ruoyi"; import Pagination from "@/components/Pagination"; import Editor from "@/components/Editor"; import FileUpload from "@/components/FileUpload"; import ImageUpload from "@/components/ImageUpload"; import ImagePreview from "@/components/ImagePreview"; import DictTag from "@/components/DictTag"; import VueMeta from "vue-meta"; import DictData from "@/components/DictData"; import * as echarts from "echarts"; import VueBarcode from "vue-barcode"; import { initWebSocket, closeWebSocket } from "@/utils/websocket"; Vue.component("downloadExcel", JsonExcel); Vue.component("barcode", VueBarcode); Vue.component("DictTag", DictTag); Vue.component("Pagination", Pagination); Vue.component("Editor", Editor); Vue.component("FileUpload", FileUpload); Vue.component("ImageUpload", ImageUpload); Vue.component("ImagePreview", ImagePreview); Vue.prototype.getDicts = getDicts; Vue.prototype.getConfigKey = getConfigKey; Vue.prototype.parseTime = parseTime; Vue.prototype.resetForm = resetForm; Vue.prototype.addDateRange = addDateRange; Vue.prototype.selectDictLabel = selectDictLabel; Vue.prototype.selectDictLabels = selectDictLabels; Vue.prototype.download = download; Vue.prototype.handleTree = handleTree; Vue.prototype.$echarts = echarts; Vue.prototype.$showNotification = function (type, title, message, onClick) { console.log('触发通知:', { type, title, message }); Vue.prototype.$notify({ title, message, type, duration: 5000, position: 'top-right', offset: 50, onClick, customClass: 'global-notification', appendTo: document.body }); }; // 监听路由变化 router.afterEach(() => { console.log('路由切换完成,当前路径:', router.currentRoute.path); }); const app = new Vue({ el: "#app", router, store, render: (h) => h(App), mounted() { const token = store.state.user.token || Cookies.get('token') || ''; if (token) { console.log('Token:', token); initWebSocket(token, (type, data) => { if (type === 'error') { Vue.prototype.$showNotification('error', '错误', data); return; } try { const message = JSON.parse(data); console.log('WebSocket 解析后消息:', message); if (message.noticeId && message.noticeTitle) { const noticeTypeLabel = message.noticeType === '1' ? '通知' : '公告'; const contentPreview = message.noticeContent ? message.noticeContent.replace(/<[^>]+>/g, '').substring(0, 20) + '...' : '无内容'; Vue.prototype.$showNotification( 'success', `新${noticeTypeLabel}`, `${message.noticeTitle} - ${contentPreview}`, () => { router.push({ path: '/redirect/notice', query: { noticeId: message.noticeId } }); } ); } else { console.log('未知消息类型:', message); Vue.prototype.$showNotification('info', '消息', '收到未知格式的消息'); } } catch (error) { console.error('消息解析失败:', error, '原始数据:', data); Vue.prototype.$showNotification('info', '消息', `服务器回应字符串: ${data}`); } }); } else { console.error('未找到 token,无法初始化 WebSocket'); } }, beforeDestroy() { closeWebSocket(); // 清理 WebSocket } }); Vue.use(directive); Vue.use(plugins); Vue.use(VueMeta); Vue.use(Print); Vue.use(Element, { size: Cookies.get("size") || "medium", }); DictData.install(); Vue.config.productionTip = false;